<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 <%@ page  import="util.*,cn.news.dao.*,cn.news.entity.*" %>
    <%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileUploadBase"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'updateNews.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
 <%!String newName(String name) {
		int pos = name.lastIndexOf('.');
		String prefix = name.substring(pos == -1 ? 0 : pos);
		return UUID.randomUUID() + prefix;

	}%>

	<%
		FileItemFactory  d=new DiskFileItemFactory();
	    ServletFileUpload up=new ServletFileUpload(d);
	    up.setSizeMax(1024*300);
	 
	  try{
	    up.setHeaderEncoding("utf-8");//设置字符集
	    
	    List<FileItem> fields=up.parseRequest(request);//拿到所有的的域
	    
	    String picPath=application.getRealPath("images");//存放路径
	    News news=new News();
	    
	    
	    for(FileItem f:fields)
	    {
	    	if(f.isFormField()){
	    	/* out.print("<h1>这是表单域,"+f.getFieldName()+",value"+f.getString("utf-8")+"</h1>");	 */
	    	
	    String fieldName=f.getFieldName();
	    	
	    	if(fieldName.equals("ntitle")){
	    		
	    		news.setNtitle(f.getString("utf-8"));
	    	}else if(fieldName.equals("nauthor")){
	    		news.setNauthor(f.getString("utf-8"));	
	    		
	    	}else if(fieldName.equals("nsummary")){
	    		news.setNsummary(f.getString("utf-8"));	
	    		
	    	}else if(fieldName.equals("ncontent")){
	    		news.setNcontent(f.getString("utf-8"));	
	    		
	    	}else if(fieldName.equals("ntid")){
	    		news.setNtid(Integer.parseInt(f.getString("utf-8")));	
	    	}else if(fieldName.equals("nid")){
	    		news.setNid(Integer.parseInt(f.getString("utf-8")));	
	    	}
	    	
	    	
	    	
	    	}else{
	    		
	    	if(f.getName()=="")	
	    		continue;
	    	
	    	/* out.print("<h1>文件域，文件名："+f.getName()+"</h1>"); */
	    	
	    	String fileName=f.getName();
	    	 List<String> fileType=Arrays.asList("gif","bmp","jpg","png");
	    	 String ext=fileName.substring(fileName.lastIndexOf(".")+1);
	    	 if(!fileType.contains(ext)){
	    		 out.print("上传失败，文件类型只能是gif,bmp,jpg,png");
	    	 }else{
	    	 
	    	String newFileName=newName(fileName);
	    	f.write(new java.io.File(picPath,newFileName));
	    	
	    	news.setNpicPath("images/"+newFileName);
	    	 }	
	    	}
	    	}
	    	
	    int result = NewsDao.DAO.modifyNews(news);

	    
	    

	    if (result>0){
	  	
	  out.println("修改新闻成功");
	    
	    }else{
	  	  out.println("修改新闻失败");  
	  	  
	    }
	    
	  }catch (FileUploadBase.SizeLimitExceededException ex) {
			out.print("上传失败，文件太大，单个文件的最大限制是：" + up.getSizeMax()
					+ "bytes!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	
	%>

	<span id="timer"> 10</span>秒返回,
	<a href="admin.jsp">返回</a>


	<script>
		var d = document.getElementById("timer").innerHTML;
		window.setInterval(function() {

			d--;
			if (d < 1) {
				window.location = "admin.jsp";

				return;
			}
			document.getElementById("timer").innerHTML = d;
		}, 1000);
	</script>
  </body>
</html>
